#include "CAnimation.h"
using namespace DarkStorm2D;
using namespace DarkStorm2D::GraphicsCore;

CAnimation::CAnimation()
{
	 m_Frames = 0;
	 m_CurrentFrame = 0;
	 m_FramesID = 0;
}

CAnimation::~CAnimation()
{
	 m_Frames = 0;
	 m_CurrentFrame = 0;
	 if ( m_FramesID != 0)
	 {
		 delete [] m_FramesID;
		 m_FramesID = 0;
	 }
}

void CAnimation::Create(CTemplateSet & TemplateSet, uint Frames, CellID* FramesID)
{
	m_TemplateSet.Create(TemplateSet.GetImage(), TemplateSet.GetCellWidth(),TemplateSet.GetCellHeight());

	m_FramesID = new CellID [Frames];

	memcpy(m_FramesID,FramesID,sizeof(CellID)*Frames);
	m_Frames = Frames;
	m_CurrentFrame = 0;
}

void CAnimation::Update()
{
	m_CurrentFrame++;
	if (m_CurrentFrame>= m_Frames)
		m_CurrentFrame = 0;
}

void CAnimation::Render(float X, float Y)
{
	CRect Rect;
	m_TemplateSet.GetRect(m_FramesID[m_CurrentFrame],&Rect);
	m_TemplateSet.GetImage()->DrawRect(X,Y,&Rect);
}

void CAnimation::SetCurrentFrame(uint Frame)
{
	m_CurrentFrame = Frame;
}

uint CAnimation::GetCurrentFrame()
{
	return m_CurrentFrame;
}